//AMD模块，需申请声明依赖
define(['common', 'hashchange', 'jqueryCookie', 'json2'], function(){
	
//	(function(){
		//后台管理界面配置
		//菜单及数据获取地址
		var systemMenuJson = 'temp/systemMenuMas.json';
		var systemMenuJson = 'temp/systemMenu.json';
		//界面加载配置
		var loadPage_target = '.pageBox';
		var loadFrameid = 'frameid';
		var loadType = false; //true 为loadpage  false为loadframe
		
		//初始化菜单
		initMenu();
	
		//logo点击
		$('.logo').on('click', function(){
			var hostName = window.location.host;
			var pathName = window.location.pathname;
			var urlName = 'http://' + hostName + pathName;
			window.location.href = urlName;
		})
		//用户工具菜单控制
		$(".header .user .info").hover(
			function () {
				$(this).find('.tools').removeClass("hidden");
			},
			function () {
				$(this).find('.tools').addClass("hidden");
			}
		);
		//工作区全局全屏按钮
		$('.section .switch').on('click', function(){
			$this = $(this).closest('.main');
			if ( $this.hasClass('fullScreen')){
				$this.removeClass('fullScreen');
				$('.switch .iconfont').html('&#474');
				browerDisFullscreen();
			}else {
				$this.addClass('fullScreen');
				$('.switch .iconfont').html('&#473');
				browerFullscreen();
			}
			eventOnResizeCur();
		})
		//系统消息层
		$('.footer .mytodo').click(function(){
			var box = '.mytodoLayer';
//			alert(box);
			if ($(box).is(':hidden')){
				$(box).fadeIn('fast');
				if ( $(box).find('iframe').length == 0 ){
//					$.rui.loading(box, '页面加载中', true);
					$.rui.loadFrame('http://mbapp.aliapp.com/sogou2014', box, 'mytodoLayer');
				}
			}else {
				$('.mytodoLayer').fadeOut('fast');
			}
		})
		//系统消息层鼠标经过事件
		var trigger = null;
		$('.mytodoLayer').hover(function(){
			clearTimeout(trigger);
		},function(){
			trigger = setTimeout(function(){
				$('.mytodoLayer').fadeOut('fast');
			},700);
		});
		$('.footer .mytodo').hover(function(){
			clearTimeout(trigger);
		},function(){
			trigger = setTimeout(function(){
				$('.mytodoLayer').fadeOut('fast');
			},700);
		});
		
		//一级模块点击事件
		$('.nav ul').on('click', 'li a', function(){
			//生成rSystem值
			var rSystem = $(this).parent().attr('hash');
			//页面url解析并修改hash
			page2hash(rSystem, '');
			//中断A跳转
			return false;
		})
		
		//左侧菜单点击事件
		$('.aside').on('click', 'dl ul li a', function(){
			//获取hash
			var active = $(this).attr('href');
			hash = $(this).closest('div[id]').attr('id');
			//页面url解析并修改hash
			page2hash(hash,active);
			//中断A跳转
			return false;
		})
		$('.aside').on('click', '.homePage', function(){
			//获取hash
//			var active = $(this).attr('href');
			hash = $(this).closest('div[id]').attr('id');
			//页面url解析并修改hash
//			alert(hash);
			page2hash(hash, '');
			//中断A跳转
			return false;
		})
		
		
		//监听浏览器尺寸及hash变化
		eventOnResizeCur();
		
		//监听浏览器窗体尺寸变化
	    $(window).on('resize', function(){
	    		eventOnResizeCur();
	    })
	
	    //监听Hash变化
		$(window).on('hashchange', function(){
			hash2page();
	    })
		
		//窗口尺寸变化时执行函数
		function eventOnResizeCur(){
			var winW = $(window).outerWidth();
			var winH = $(window).outerHeight();
			var mainH = $(window).outerHeight() - $('.header').outerHeight() - $('.footer').outerHeight();
			var fullScreenH = $(window).outerHeight() - 0;
			//侧栏高度
			$('.main').height(mainH);
			$('.aside').height(mainH);
			//工作区高度，判断是否全屏和是否有侧栏
			$this = $('.main .section');
			if ( $('.main').hasClass('fullScreen') ){
				$this.css('height', fullScreenH );
				$this.css('margin-left', 0);
			}else {
				$this.height(mainH);
				$this.css('margin-left', '');
			}
			$('.pageBox').height($this.height());
		}
		function eventOnHashChangeCur(){
			hash2page();
		}
		
		function initMenu(){
			//默认菜单数据从本地cookie读取
			var systemMenu = $.cookie('menu');
//			console.log("取到cookie内容为：" + systemMenu);
			if ( systemMenu==null || systemMenu=='undefined'){
//				console.log("cookie为空或未定义menu，准备取json");
				$.getJSON(systemMenuJson, function(data){
//					console.log("取到json，数据类型是："+ typeof data + "准备转为字符串");
					systemMenu = JSON.stringify(data);
//					$.cookie('menu', systemMenu);
					rederDom(systemMenu);
				})
			}else {
				rederDom(systemMenu);
			}
			
			function rederDom(data){
//				console.log("渲染dom开始"+ data);
				systemMenu = JSON.parse(data);
				//系统信息
				document.title = systemMenu.name;
				$('.header .logo h1').html(systemMenu.name);
				$('.header .logo small').html(systemMenu.subName);
				$('.header .logo img').attr("src", systemMenu.icon);
				$('.header .user .name').html(systemMenu.userName);
				$('.header .user .role').html(systemMenu.userRole);
				if (!systemMenu.userAvatar){
					$('.header .user .avatar').addClass('hidden');
				}else {
					$('.header .user .avatar img').attr('src', systemMenu.userAvatar);
				}
				$('.footer .copyright').html(systemMenu.copyRight);
				
				//一级模块
				$.each(systemMenu.menu, function(name, value) {
					var navLi = "<li hash="+ this.cname +" ><a href=" + this.url + "><img src=" + this.icon + "><h5>" + this.name + "</h5></a>";
					$('.nav ul').append(navLi);
					//二级模块
					hash = this.cname;
					sub = this.sub;
					url = this.url;
					if (sub){
						var subNav = "<div class=\'subNav\' id=\'" + hash + "\'></div>";     
						$('.aside').append(subNav);
						if ( url!=''){
//							console.log(url);
							var rSystemHome = "<a href=#" + hash + " class=\"homePage\" ><i class=\"ionicons\">&#xf20a;</i>首页</a>";
							$("#"+ hash).append(rSystemHome);
						}
						
						$.each(sub, function(name, value) {
							if(!this.icon){
								this.icon = "&#390";
							}
							$("#" + hash).append("<dl><dt><i class=\"ionicons\">" + this.icon + "</i>" + this.name + "</dt><dd><ul id=\'" + this.cname + "\'></ul></dd></dl>");
							//三级页面
							hash2 = this.cname;
							sub2 = this.sub;
							$.each(sub2, function(name, value){
								var subNav = "<li><a href=" + this.url + "><i class=\"ionicons\">&#xf12f;</i>" + this.name + "</a>";
								$("#"+ hash2).append(subNav);
							})
						});
						
					}
				});
				//DOM填充后进行页面寻路
				hash2page();
			}
			
		}
		
		function hash2page(){
			//获取hash
			var hash = window.location.hash.split('#')[1];
			
			var rSystem, rPage
			
			//判断是否需要进行寻路，并确定一级模块
			if (hash==null){
				//hash为空则找到第一个一级模块
				rSystem = $('.nav li').eq(0).attr('hash');
			}else if (hash.indexOf('here') >= 0){
				history.back();
			}else {
				//对hash进行分词，得到每一级参数的内容，确定一级模块节点
				rSystem = hash.split('&')[0];
//				rModule = hash.split('&')[1];
				rPage = hash.split('&')[1];
			}
			
//			console.log('hash-初始化:'+rSystem+'+'+rPage);
			
			//激活匹配的系统菜单
			$('.nav li').removeClass('act');
			$('.nav li[hash="'+rSystem+'"]').addClass('act');
			
			//是否开启侧栏
			if ( $('.subNav[id="'+rSystem+'"]').length > 0){
				$('.main').removeClass('noSide');
				$('.aside .subNav').hide();
				var sideNav = $('.subNav[id="'+rSystem+'"]');
				sideNav.show();
				$('.subNav[id="'+rSystem+'"] li, .subNav[id="'+rSystem+'"] .homePage').removeClass('act');
				//解析rPage内容，确定激活链接
				if (rPage != null  && rPage !='undefined'){
					sidelink = sideNav.find('a[href="'+rPage+'"]');
					sidelink.parent().addClass('act');
				}else {
					//hash中rPage为空处理
					if ( $('.nav li[hash="'+rSystem+'"] a').attr('href').length > 0){
						sidelink = $('.nav li[hash="'+rSystem+'"] a');
						sideNav.find('.homePage').addClass('act');
						
					}else {
						sidelink = sideNav.find('dd').eq(0).find('li').eq(0).find('a');
						sidelink.parent().addClass('act');
					}
				}
				target_page = sidelink.attr('href');
			}else {
				$('.main').addClass('noSide');
				if ( $('.nav li[hash="'+rSystem+'"]').length > 0){
					target_page = $('.nav li[hash="'+rSystem+'"]').find('a').attr('href');
				}else {
					target_page = rPage;
				}
			}
			
			//地址增加时间戳，防止缓存
			target_page = target_page + "?" + new Date().getTime();
			
			//开始局部显示模态加载动画
			$.rui.msg('页面加载中', true, true);
			
			//加载页面判断是否为代码片段
			if ( loadType == true){
				if ( target_page.indexOf('http:') >= 0  ){
					$.rui.loadFrame(target_page,loadPage_target,loadFrameid);
				}else{
					$.rui.loadPage(target_page,loadPage_target);
				}	
			}else{
				$.rui.loadFrame(target_page,loadPage_target,loadFrameid);	
			}
		}
		
		function page2hash(rSystem, rPage){
			if (rPage.length > 0){
				rPage = '&' + rPage;
			}else {
				rPage = '';
			}
			hash = rSystem + rPage;
			curHash = window.location.hash.split('#')[1];
			
			if ( hash == curHash ){
				window.location.reload();
			}else{
				window.location.hash = '#' + hash;
			}
			
		}
		
		function browerFullscreen(){
			var docElm = document.documentElement; 
			//W3C 
			if (docElm.requestFullscreen) { 
				docElm.requestFullscreen(); 
			} 
			//FireFox 
			else if (docElm.mozRequestFullScreen) { 
				docElm.mozRequestFullScreen(); 
			} 
			//Chrome等 
			else if (docElm.webkitRequestFullScreen) { 
				docElm.webkitRequestFullScreen(); 
			} 
			//IE11 
			else if (docElm.msRequestFullscreen) { 
				docElm.msRequestFullscreen(); 
			} 
		}
		
		function browerDisFullscreen(){
			if (document.exitFullscreen) { 
				document.exitFullscreen(); 
			} 
			else if (document.mozCancelFullScreen) { 
				document.mozCancelFullScreen(); 
			} 
			else if (document.webkitCancelFullScreen) { 
				document.webkitCancelFullScreen(); 
			} 
			else if (document.msExitFullscreen) { 
				document.msExitFullscreen(); 
			} 
		}
		
//	})()
	
	
})